import Vue from 'vue';
import Vuex from 'vuex';
import {getData, setData, removeData} from "../utils/auth";

Vue.use(Vuex);
let initData = getData();
const state = {
    isLogin: false,
    data: {}
}

if (typeof initData === 'object' && Object.prototype.toString.call(initData) === "[object Object]" && JSON.stringify(initData) !== '{}' && Object.keys(initData).length > 0) {
    state.isLogin = true;
    state.data = initData;
} else {
    removeData();
}

const mutations = {
    login(state, data) {
        state.isLogin = true;
        state.data = data;
        setData(data);
    },
    logout(state) {
        state.isLogin = false;
        state.data = {};
        removeData();
    }
};

export default new Vuex.Store({
    state,
    mutations
});